import GameEvent from "../../Commons/GameEvent";
import { getZeroTime, randomRange } from "../../GlobalFunction";
import AudioManager, { AudioEffectType } from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import EquipManager from "../Equips/EquipManager";
import PetManager from "../Pets/PetManager";
import TaskManager from "../Tasks/TaskManager";
import ShopEntity from "./ShopEntity";
import ShopTemplate from "./ShopTemplate";

export enum ShopEvent {
    BUYGOODS_EVENT = "buyGoods_event"
}

export default class ShopManager implements IModelManager {


    public moveSpeedTime: number = 0;
    public attackTime: number = 0;

    private static _single: ShopManager = null;
    private shopMap: Map<number, ShopTemplate>;
    public static get single(): ShopManager {
        if (ShopManager._single == null) {
            ShopManager._single = new ShopManager();
        }
        return ShopManager._single;
    }

    updata(dt) {
        if (this.moveSpeedTime > 0) {
            this.moveSpeedTime -= dt;
        }

        if (this.attackTime > 0) {
            this.attackTime -= dt;
        }
    }


    initConfig(): void {
        this.shopMap = new Map();
        var jsonAss = cc.resources.get("configs/shop", cc.JsonAsset);
        for (const iterator of jsonAss.json) {
            var tp = new ShopTemplate();
            tp.init(iterator);
            this.shopMap.set(tp.id, tp);
        }
    }
    getShopTemplateArr(): ShopTemplate[] {
        var arr: ShopTemplate[] = [];
        this.shopMap.forEach((value: ShopTemplate, key: number, map: Map<number, ShopTemplate>) => {
            arr.push(value);
        }, this);

        return arr;
    }

    getShopTemplate(id: number): ShopTemplate {
        return this.shopMap.get(id);
    }

    getShopEntitiy(id: number): ShopEntity {
        return SaveManager.single.userdata.getShopEntity(id);
    }

    requestResetShop() {
        var userdata = SaveManager.single.userdata;
        let leftTime = cc.sys.now() - userdata.shopResetTime;
        if (leftTime >= 24 * 60 * 60 * 1000) {//
            userdata.shopResetTime = getZeroTime();
            for (const iterator of userdata.shopList) {
                iterator.buyTimes = 0;
                iterator.endTime = 0;
            }

            SaveManager.single.saveData();
        }
    }

    requestBuy(id: number) {
        var userdata = SaveManager.single.userdata;
        var ey = userdata.getShopEntity(id);
        if (ey != null && ey.buyTimes >= 10) {
            UIManager.single.showTips("今天购买次数已满。");
            return;
        }
        var shoptp = this.getShopTemplate(id);
        if (shoptp.costType == 1) {
            userdata.gold -= shoptp.cost;
            GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
        } else if (shoptp.costType == 2) {
            userdata.diamond -= shoptp.cost;
            GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
        }

        if (ey == null) {
            ey = new ShopEntity({ id: id, endTime: cc.sys.now() * 0.001, buyTimes: 0 });
            userdata.shopList.push(ey);
        }
        ey.buyTimes += 1;
        ey.endTime = cc.sys.now() * 0.001 + 3 * 60;
        switch (id) {
            case 101:
                userdata.gold += 10000;
                TaskManager.single.requestUpdataTaskPregress(103, 10000);
                UIManager.single.showTips("金币 +10000。");
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
                AudioManager.single.playEffect(AudioEffectType.goldDrop);
                break;
            case 102:
                userdata.diamond += 100;
                UIManager.single.showTips("钻石 +100。");
                GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
                TaskManager.single.requestUpdataTaskPregress(109, 100);
                break;
            case 103://移速+50%（5分钟）
                // var arr = EquipManager.single.getEquipTemplateArr();
                // var index = randomRange(0, arr.length - 1);
                // EquipManager.single.requestAddEquip(arr[index].id);
                ShopManager.single.moveSpeedTime = 60 * 3;
                break;
            case 104:
                var that = this;
                for (let i = 0; i < 10; i++) {
                    setTimeout(() => {
                        that.getEquip();
                    }, 150 * i);
                }
                break;
            case 105://攻击+50%（5分钟）
                //this.getPet();
                ShopManager.single.attackTime = 60 * 3;
                break;
            case 106:
                var that = this;
                for (let i = 0; i < 10; i++) {
                    setTimeout(() => {
                        that.getPet();
                    }, 150 * i);
                }
                break;
        }
        SaveManager.single.saveData();
        TaskManager.single.requestUpdataTaskPregress(107, 1);
    }
    getEquip() {
        var arr = EquipManager.single.getEquipTemplateArr();
        var index = randomRange(0, arr.length - 1);
        EquipManager.single.requestAddEquip(arr[index].id);
    }

    getPet() {
        var arr = PetManager.single.getPetTemplateArr();
        var index = randomRange(0, arr.length - 1);
        PetManager.single.requestGetPet(arr[index].id);
    }


}
